#include "head\TPL0401A.h"
#include "head\MCU.h"

void TPL_IIC_Init()
{
    /*
     * 配置IIC复用引脚
     */
    GPIO_setAsPeripheralModuleFunctionInputPin(TPL_IIC_PORT,TPL_IIC_PIN_SDA + TPL_IIC_PIN_SCL);

    /*
     * 初始化IIC主模式
     */
    USCI_B_I2C_initMasterParam param = {0};
    param.selectClockSource = USCI_B_I2C_CLOCKSOURCE_SMCLK;
    param.i2cClk = UCS_getSMCLK(); //返回一个u32，此时SMCLK的时钟频率
    param.dataRate = USCI_B_I2C_SET_DATA_RATE_400KBPS; //选择传输速度，有100和400两种选择
    USCI_B_I2C_initMaster(TPL_IIC_BASE, &param);

    /*
     * 指定从机地址
     */
    USCI_B_I2C_setSlaveAddress(TPL_IIC_BASE,TPL0401A_ADDR);

    USCI_B_I2C_enable(TPL_IIC_BASE);
    TPL0401A_Init();
}


void TPL0401A_Init()
{
    TPL_SetValueD(4.5);
}

void TPL_SetValueD(float v)
{
    if(v >= 12.0)
    {
        v = 12.0;
    }
    else if(v <= 4.5)
    {
        v = 4.5;
    }
    //float a = 8.8517/(v-1.238)-0.5;
    //float r = (453000.0*a)/(453.0-a);
    //uint8_t d = (uint8_t)((128.0*r/10000)+0.5);//v=2.0,d=145.869
    double a = 8.6/(v-1.238)-0.5;
    double r = (472000.0*a)/(472.0-a);
    uint8_t d = (uint8_t)((128.0*r/10000)+0.5);//v=2.0,d=145.869
    TPL_Write_Registers(d);
}

void TPL_Write_Registers(uint8_t Hex)
{
    uint8_t Timeing = 6;
    //Enable transmit Interrupt
    USCI_B_I2C_clearInterrupt(TPL_IIC_BASE,
        USCI_B_I2C_TRANSMIT_INTERRUPT | USCI_B_I2C_RECEIVE_INTERRUPT);//清除中断标志，及 UCBxIFG 置0
    USCI_B_I2C_enableInterrupt(TPL_IIC_BASE,
        USCI_B_I2C_TRANSMIT_INTERRUPT);//v | USCI_B_I2C_RECEIVE_INTERRUPT);//设置中断使能，及 UCBxIE 置1

    //Delay between each transaction
    __delay_cycles(50);

    TPL_TXByteCtrTotal = 2-1;              //总共要发送2个字节，下面的USCI_B_I2C_masterSendMultiByteStart函数会发出一个且不进中断，所以这里减去1

    TPL_TxDataBuff[0] = 0x00;              //将要发送的数据1，寄存器地址,TPL模块只有一个寄存器
    TPL_TxDataBuff[1] = Hex;              //将要发送的数据2，寄存器数值
    P_TPL_TxData = (uint8_t *)TPL_TxDataBuff;//指向将要发送的数据的开头

    USCI_B_I2C_masterSendMultiByteStart(TPL_IIC_BASE,*P_TPL_TxData++);//发送S、AD+W与第一个字节（陀螺仪的寄存器地址）
    //等待停止信号发送完毕
    do{
        __delay_cycles(MSP430_FREQUENCY/2);
        Timeing--;
        if(Timeing == 0)
        {
            __no_operation();
        }
    }
    while(TPL_TXByteCtrTotal != -1);//当最后一个字节发送完毕后，TPL_TXByteCtrTotal会等于-1
}

//void TPL_powerON()
//{
//    GPIO_setOutputHighOnPin(TPL_POW_PORT,TPL_POW_PIN);
//}
//
//void TPL_powerOFF()
//{
//    GPIO_setOutputLowOnPin(TPL_POW_PORT,TPL_POW_PIN);
//}

//void TPL_powerOutON()
//{
//    GPIO_setOutputHighOnPin(TPL_POW_OUT_PORT,TPL_POW_OUT_PIN);
//}
//
//void TPL_powerOutOFF()
//{
//    GPIO_setOutputLowOnPin(TPL_POW_OUT_PORT,TPL_POW_OUT_PIN);
//}

//******************************************************************************
//
//This is the USCI_B3 interrupt vector service routine.
//
//******************************************************************************
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=USCI_B3_VECTOR
__interrupt
#elif defined(__GNUC__)
__attribute__((interrupt(USCI_B3_VECTOR)))
#endif
void TPL_USCI_B3_ISR (void)
{
    switch (__even_in_range(UCB3IV,12)){
        case USCI_I2C_UCTXIFG:
        {
            //Check TX byte counter
            if (TPL_TXByteCtrTotal > 0)
            {
                //Initiate send of character from Master to Sla
                USCI_B_I2C_masterSendMultiByteNext(TPL_IIC_BASE,*P_TPL_TxData++);
                //Increment TX byte counter
                TPL_TXByteCtrTotal--;
            }
            else
            {
                //Initiate stop only
                USCI_B_I2C_masterSendMultiByteStop(TPL_IIC_BASE);
                //Clear master interrupt status
                USCI_B_I2C_clearInterrupt(TPL_IIC_BASE,USCI_B_I2C_TRANSMIT_INTERRUPT);
                TPL_TXByteCtrTotal--;
            }
            break;
        }
        case USCI_I2C_UCRXIFG:
        {
            __no_operation();
        }
        default:break;
    }
}
